/**
 * Created with Vim7.3 ubuntu12.04
 * @fileOverview : 
 * @author : Lyle <lylechen2014@gmial.com>
 * @since : 2014-11-25 14:34:23
 * @filename : controller/index.js
 * @version : 
 * @description : 
 */
define(function(require, exports, module){
   var  tplTop = require('{view}top.tpl'),
        tplLeft = require('{view}left.tpl'),
        tplRight = require('{view}right.tpl'),
        ztreeS = require('{controller}ztree');

    var IndexView = Backbone.View.extend({
    
        el: 'body',

        initialize: function (obj) {
            if(window.location.hash == '') {
               Backbone.routerObj.navigate('index/w', { trigger: true });
               return;
            };
            this.run(obj);
            this.bindEvt();
            this.ztreeRun(obj);
        },

        tplRender : function() {
            //top渲染
            if ($('#index_top').length < 1) {           
                $('body').append(_.template(tplTop)({'resourceUrl':seajs.data.vars.resourceUrl}));
            } else {
                $('#index_top').css('display','block');
            } 

            //left渲染
            if ($('#index_left').length < 1) { 
                $('body').append(_.template(tplLeft));
            } else {
                $('#index_left').css('display','block');
            } 

            //right渲染 
            if ($('#index_right').length < 1) { 
                $('body').append(_.template(tplRight));
            } else {
                $('#index_right').css('display','block');
            } 
        },

        defalutModule : function(obj) {
            if (obj.name == 'index' && obj.val == undefined) {
                Backbone.routerObj.navigate('gold_monitor/i', { trigger: true });
            } else if (obj.val !== undefined) {
                Backbone.routerObj.navigate(obj.val+'/i', { trigger: true });
            }
        },

        defalutData : function(obj) {
            var tthis = this, data = share.getLocalStorage("siteInfo");
            if (data == null || data == '') {
                var ajaxOpt = {
                    url : seajs.data.vars.apiUrl+'sites_info',
                    type : 'POST',
                    success : function(data) {
                        var siteInfo = {};
                        _.each(data,function(obj,i){siteInfo[obj.id] = {name:obj.name,allname:obj.descr,index:i,id:obj.id};});
                        share.setLocalStorage("siteInfo",siteInfo);
                        tthis.defalutModule(obj);
                    },
                    error : function(xhr, errorType, error) {
                        alert('http request failed!'); 
                    },
                    complete : function(xhr,status){
                    }
                };
                share.ajax(ajaxOpt);
           } else {
             tthis.defalutModule(obj);
           }
        },

        bindEvt : function() {
        },

        run: function(obj) {
            //console.log('run',obj);
            this.tplRender();
            this.defalutData(obj);
        },

        ztreeRun : function(obj) {
            ztreeS.init(obj);
        }
	});

	module.exports = IndexView;
})

